
package com.market.web.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.market.bean.Lineitem;
import com.market.bean.Orders;
import com.market.bean.Product;
import com.market.web.form.LineItemForm;
import com.market.web.form.OrdersForm;
import com.market.web.utils.ListUtils;

public class AdminOrViewAction extends AdminOrdersAction
{
    public String execute() throws Exception
    {
        long id = getLong("id");
        String action = getRequest().getParameter("action");
        Orders or = getOrdersService().findById(id);
        OrdersForm orf = new OrdersForm();
        BeanUtils.copyProperties(or, orf);
        List l = new ArrayList(or.getLineitems());
        List lf = ListUtils.convert(l, Lineitem.class, LineItemForm.class);
        float total = 0;
        for (int i = 0; i < l.size(); i++)
        {
            Lineitem line = (Lineitem)l.get(i);
            LineItemForm linef = (LineItemForm)lf.get(i);
            total += line.getPrice() * line.getAmount();
            Product pro = getProductService().findById(line.getProductid());
            linef.setProname(pro.getName());
            linef.setUnit(pro.getUnit());
            linef.setTotal(total);
            lf.set(i, linef);
        }
        orf.setTotal(total);
        getRequest().setAttribute("order", orf);
        getRequest().setAttribute("line", lf);
        if ("process".equals(action))
        {
            return "orderprocess";
            //return mapping.findForward("orderprocess");
        }
        else
        {
            return "orderview";
            //return mapping.findForward("orderview");
        }
    }
}
